package com.dede.test

import android.view.View

/**
 * @author hsh
 * @date 2017/10/10 13:48.
 * @doc 防止点击事件短时间内多次回调
 */
abstract class OnNoDoubleClickListener(minDelayTime: Int) : View.OnClickListener {

    private var delayTime = minDelayTime
    private var lastClickTime: Long = 0

    constructor() : this(1000)

    override fun onClick(view: View) {
        val timeMillis = System.currentTimeMillis()
        if (lastClickTime + delayTime < timeMillis) {
            onNoDoubleClick(view)
            lastClickTime = timeMillis
        }
    }

    abstract fun onNoDoubleClick(view: View)
}

fun View.onNoDoubleClick(onNoDoubleClick: (View) -> Unit) {
    setOnClickListener(object : OnNoDoubleClickListener() {
        override fun onNoDoubleClick(view: View) {
            onNoDoubleClick(view)
        }
    })
}

fun View.onNoDoubleClick(minDelayTime: Int, onNoDoubleClick: (View) -> Unit) {
    setOnClickListener(object : OnNoDoubleClickListener(minDelayTime) {
        override fun onNoDoubleClick(view: View) {
            onNoDoubleClick(view)
        }
    })
}